import UserSelector from '@/pages/biz/proj/common/UserSelector'
export default {
    name: "Index",
    components: {
      UserSelector
    },
    data() {
        return {
          allItemList:[],
          showItemList:[],
          formShow: false,
            searchForm: {
              startTime: '',
              endTime: '',
              consignPerson:'',
              targetPerson:''
            },
            ids: [],
            multi: true,
            tableData: [],
            pager: {
                page: 0,
                size: 20,
                total: 0
            },
            dialogFormVisible: false,
            viewHide: false,
            editCode: false,
            consignPerson: [],
            targetPerson: [],
            taskConsign: {
                consignPerson: '',
                consignPersonName: '',
                targetPerson: '',
                targetPersonName: '',
                isUse: '1',
                startTime: '',
                endTime: '',
                createPerson: '',
                createTime: '',
            },
            consignState: [{key:"1",label:"启用"},{key:"0",label:"禁用"}],
            dialogTitle: '新增委托',
            entityId: '',
            useable: true,
            expireTimeOption: this.checkDate(), //searchForm
            expireTimeOption1: this.checkDate1() //entityForm
        }
    },
    computed: {
        height() {
            return `calc(100vh - 350px)`;
        }
    },
    mounted() {
        this.getList();
    },
    methods: {
      //重置
      resetForm() {
        this.handleFormClose();
      },
        // 关闭弹窗
        handleFormClose: function () {
            this.searchForm = {
                startTime: '',
                endTime: ''
            };
          this.consignPerson = [];
          this.targetPerson = [];
            this.taskConsign  ={
                id: '',
                consignPerson: '',
                consignPersonName: '',
                targetPerson: '',
                targetPersonName: '',
                isUse: '1',
                startTime: '',
                endTime: '',
                createPerson: '',
                createTime: '',
            },

            this.viewHide = false;
            this.editCode = false;
            this.dialogTitle = '新增委托';
            this.dialogFormVisible = false;
            this.entityId = '';
        },
        // 查看
        view: function (item) {
            this.dialogTitle = '查看委托';
            this.dialogFormVisible = true;
            this.viewHide = true;
            this.editCode = true;
            this.taskConsign = {
                consignPerson: item.consignPerson || '',
                consignPersonName: item.consignPersonName || '',
                targetPerson: item.targetPerson || '',
                targetPersonName: item.targetPersonName || '',
                isUse: item.isUse || '1',
                startTime: item.startTime || '',
                endTime: item.endTime || '',
                createPerson: item.createPerson || '',
                createTime: item.createTime || ''
            }
            if(item.consignPerson != null){
              this.consignPerson.push(item.consignPerson);
            }
            if(item.targetPerson != null){
              this.targetPerson.push(item.targetPerson);
            }
        },
        // 修改
        edit: function (item) {

            this.dialogTitle = '修改委托';
            this.dialogFormVisible = true;
            this.editCode = false;
            this.entityId = item.id;
            this.taskConsign = {
                consignPerson: item.consignPerson || '',
                consignPersonName: item.consignPersonName || '',
                targetPerson: item.targetPerson || '',
                targetPersonName: item.targetPersonName || '',
                isUse: item.isUse || '1',
                startTime: item.startTime || '',
                endTime: item.endTime || '',
                createPerson: item.createPerson || '',
                createTime: item.createTime || new Date(),
            };
          if(item.consignPerson != null){
            this.consignPerson.push(item.consignPersonName);
          }
          if(item.targetPerson != null){
               this.targetPerson.push(item.targetPersonName);
          }
        },
        // 删除
        deleteBatch: function () {
            if (this.ids.length > 0) {
                this.delete();
            } else {
              this.$message({
                type: 'warning',
                message: '请先选择数据!'
              });
            }

        },
        delete: function () {
            this.$confirm('此操作将永久删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$api.post('/api/biz/flowMyWork/delBatchTaskConsign', this.ids).then(res => {
                    if (res.retHead.code === '0000000') {
                        if(res.retBody.status==="0"){
                            this.getList();
                            this.$message({
                                type: "success",
                                message: "删除成功"
                              });
                        }else{
                          this.$message({
                            type: "error",
                            message: res.retBody.message
                          });
                        }
                          
                      }else{
                        this.$message({
                          type: "error",
                          message: res.retHead.message
                        });
                      }
                    
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 保存
        doSave: function () {
            let params = {};
            let user = sessionStorage.getItem('user') ? JSON.parse(sessionStorage.getItem('user')) : null;
            if (JSON.stringify(user) === "{}") {
                user = null;
            }


            params = {

                consignPerson: this.taskConsign.consignPerson || '',
                consignPersonName: this.taskConsign.consignPersonName || '',
                targetPerson: this.taskConsign.targetPerson || '',
                targetPersonName: this.taskConsign.targetPersonName || '',
                isUse: this.taskConsign.isUse || '1',
                startTime: this.taskConsign.startTime || '',
                endTime: this.taskConsign.endTime || '',
                createPerson: this.taskConsign.createPerson || user.userid,
                createTime: this.taskConsign.createTime || new Date(),

            };


            if (this.entityId === '') {

            } else {
                params.id = this.entityId;
            }
            
           this.$api.post(   '/api/biz/flowMyWork/saveTaskConsign', params).then(res => {
            if (res.retHead.code === '0000000') {
                if(res.retBody.status==="0"){
                    this.handleFormClose();
                    this.$message({
                        type: 'success',
                        message: '保存成功!'
                    });
                    this.getList();
                }else{
                  this.$message({
                    type: "error",
                    message: res.retBody.message
                  });
                }
                  
              }else{
                this.$message({
                  type: "error",
                  message: res.retHead.message
                });
              }
                
            });
        },
        // 获取委托列表
        getList: function () {
            console.log(this.searchForm);
            let params = {
                page: this.pager.page === 0 ? this.pager.page : this.pager.page - 1,
                size: this.pager.size,
                startTime: this.searchForm.startTime || '',
                endTime: this.searchForm.endTime || '',
            };
            if(this.searchForm.consignPerson){
              params.consignPerson=this.searchForm.consignPerson;
            }
            if(this.searchForm.targetPerson){
              params.targetPerson=this.searchForm.targetPerson
            }
            this.$api.post( '/api/biz/flowMyWork/getTaskConsignList', params).then(res => {
                if (res.retHead.code === '0000000') {
                    if(res.retBody.status==="0"){
                        this.tableData = res.retBody.data.resContent;
                        this.pager.total = res.retBody.data.total;
                    }else{
                      this.$message({
                        type: "error",
                        message: res.retBody.message
                      });
                    }
                      
                  }else{
                    this.$message({
                      type: "error",
                      message: res.retHead.message
                    });
                  }
            });
        },
        handleSelectionChange: function (val) {
            this.ids = val;
        },
        stateChange: function (val) {
            this.taskConsign.isUse = val == true ? "1" : "0";
        },
        selectConsignPerson2: function (data) {
            if(data.length > 0){
              this.searchForm.consignPerson = data[0].value;
              this.searchForm.consignPersonName = data[0].label;
            }
  
          },
          selectTargetPerson2: function (data) {
            if(data.length > 0){
              this.searchForm.targetPerson = data[0].value;
              this.searchForm.targetPersonName = data[0].label;
            }
          },
        selectConsignPerson: function (data) {
          if(data.length > 0){
            this.taskConsign.consignPerson = data[0].value;
            this.taskConsign.consignPersonName = data[0].label;
          }

        },
        selectTargetPerson: function (data) {
          if(data.length > 0){
            this.taskConsign.targetPerson = data[0].value;
            this.taskConsign.targetPersonName = data[0].label;
          }
        },
        startTimeChange() {
            this.searchForm.endTime = '';
        },
        startTimeChange1() {
            this.taskConsign.endTime = '';
        },
        checkDate: function () {
            let self = this;
            return {
                disabledDate(date) {

                    let t0 = Date.now();
                    if (self.searchForm.startTime != '') {
                        let t1 = new Date(self.searchForm.startTime).getTime();
                        if (t0 > t1) {
                            return date.getTime() < t0;
                        }
                        return date.getTime() < t1;
                    } else {
                        return date.getTime() < t0;
                    }


                }
            };
        },
        checkDate1: function () {
            let self = this;
            return {
                disabledDate(date) {

                    let t0 = Date.now();
                    if (self.taskConsign.startTime != '') {
                        let t1 = new Date(self.taskConsign.startTime).getTime();
                        if (t0 > t1) {
                            return date.getTime() < t0;
                        }
                        return date.getTime() < t1;
                    } else {
                        return date.getTime() < t0;
                    }


                }
            };
        },
        dateFormatter: function (row, column) {
            let datetime = row[column.property];
            if(datetime){
                datetime = new Date(datetime);
                let y = datetime.getFullYear() + '-';
                let mon = datetime.getMonth()+1 + '-';
                let d = datetime.getDate();
                return y + mon + d;
            }
            return ''
        },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pager.size = val;
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.pager.page = val;
      },
      
        handleHelp () {
            POBrowser.openWindowModeless('http://172.16.77.125:9009/api/office/openFile?fileName=test.docx','width=1200px;height=800px;');
          }
     
    }
}
